package ext.sim.forces;

import ext.sim.Body;
import java.util.ArrayList;
import math.math;
import math.vec3;
import static math.vec3.lengthSq;
import static math.vec3.sub;

public class ForceGravCentered implements Force
{
    
    private ArrayList<Body> rBodies;
    
    private vec3 mCenterPt;
    
    private double mCenterMass;
    
    
    public ForceGravCentered(
            ArrayList<Body> aBodies, 
            vec3 aCenterPt, 
            double aCenterMass)
    {
        this.rBodies = aBodies;
        this.mCenterPt = aCenterPt;
        this.mCenterMass = aCenterMass;
    }
    
    
    public void apply()
    {
        for (Body b : this.rBodies)
        {
            vec3 R = sub(this.mCenterPt, b.mX);
            double sqR = lengthSq(R);
            R.mul(math.rsqrt(sqR)); // Normalize R.
            
            double fStrength = this.mCenterMass * b.mMass * this.mCenterMass / sqR;
            
            b.mF.addmul(fStrength, R);
        }
    }
    
}
